<template>
  <div class="mainsu">
    <div class="main_spac1">
      <div class="main_white padding10 paddingBottom20">
        <div class="mainor_form small-top">
          <el-form
            :inline="true"
            :model="form"
            class="demo-form-inline"
            ref="userListFrom"
            label-width="90px"
            id="form"
          >
            <el-form-item label="状态：" prop="status">
              <el-select
                v-model.trim="form.status"
                placeholder="状 态"
                class="wid220"
                @change="getArticleList"
              >
                <el-option label="所有" :value="parseInt(0)"></el-option>
                <el-option
                  v-for="(statusList, index) in statusList"
                  :key="index"
                  :label="statusList"
                  :value="parseInt(index + 1)"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="申请日期：" prop="createTime">
              <el-date-picker
                v-model="createTime"
                type="daterange"
                class="time220"
                :editable="false"
                unlink-panels
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd"
                :clearable="false"
                @change="getArticleList"
                :picker-options="dateOption"
              />
            </el-form-item>
            <el-form-item label="用户：" prop="uid">
              <!-- <change-user-input
                ref="changeUserInput"
                @sendValue="getUser"
                @clear="clearUid"
              ></change-user-input> -->
              <change-user-input
                ref="changeUserInput_form"
                @sendValue="
                  (data) => {
                    form.uid = data.info.user_id;
                  }
                "
                @zidname="uidName"
                @clear="clearzid"
              ></change-user-input>
            </el-form-item>
            <MainFormBtn @reset="reset" @getSearchFor="getSearchFor" :loading="listLoading" :showBtn="moredata">
              <el-button @click="exportMethodDia((exportFormVisible = true))">导出</el-button>
            </MainFormBtn>
            <div class="more el-form-item" @click="more">
              <span>更多</span>
              <i :class="[{ on: moredata }, ' el-icon-arrow-down']"></i>
            </div>
            <div class="clearfix"></div>

            <el-collapse-transition>
              <div v-show="moredata">
                <el-form-item label="子账号：" prop="zid">
                  <el-select
                    v-model="form.zid"
                    placeholder="子账号"
                    class="wid220"
                    no-data-text
                    filterable
                    @change="getArticleList"
                  >
                    <el-option
                      v-for="(zid_time, index) in zid_name"
                      :key="index"
                      :label="zid_time.nickname"
                      :value="zid_time.id"
                    ></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="商品：" prop="goods_spu_id" style="margin-right: 0">
                  <change-goods-input
                    ref="changeGoodsInput"
                    @clear="
                      () => {
                        this.form.goods_spu_id = '';
                      }
                    "
                    @sendValue="
                      (data) => {
                        this.form.goods_spu_id = data.id;
                        getArticleList();
                      }
                    "
                  ></change-goods-input>
                </el-form-item>
                <el-form-item label="退货单号：" prop="order_sn">
                  <el-input
                    v-model.trim="form.order_sn"
                    placeholder="退货单号"
                    clearable
                    class="wid220"
                  ></el-input>
                </el-form-item>
                <el-form-item label="订单号：" prop="related_order_sn">
                  <el-input
                    v-model.trim="form.related_order_sn"
                    placeholder="关联订单号"
                    clearable
                    class="wid220"
                  ></el-input>
                </el-form-item>
                <div class="clearfix"></div>
                <el-form-item label="入库单号：" prop="storage_order_sn">
                  <el-input
                    v-model.trim="form.storage_order_sn"
                    placeholder="关联入库单号"
                    clearable
                    class="wid220"
                  ></el-input>
                </el-form-item>
                <el-form-item label="审核日期：" prop="check_createTime">
                  <el-date-picker
                    v-model="check_createTime"
                    type="daterange"
                    class="time220"
                    :editable="false"
                    unlink-panels
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    value-format="yyyy-MM-dd"
                    :clearable="false"
                    :picker-options="dateOption"
                    @change="getArticleList"
                  />
                </el-form-item>
                <div class="clearfix"></div>
                <el-form-item label=" ">
                  <MainFormBtn class="btn-bottom" @reset="reset" @getSearchFor="getSearchFor" :loading="listLoading" :showBtn="!moredata">
                    <el-button @click="exportMethodDia((exportFormVisible = true))">导出</el-button>
                  </MainFormBtn>
                </el-form-item>
              </div>
            </el-collapse-transition>
          </el-form>
        </div>
        <div class="main-controls-wrap">
          <div class="mainor_sec">
            <router-link to="/order/return/add" class="mainor_tea">创建退货单</router-link>
          </div>
        </div>

        <MainTable
          ref="multipleTable"
          showSelection
          :loading="listLoading"
          :columns="orderReturnColumns"
          :tableData="list.data"
          :height="borderHeight"
          @selection-change="handleSelectionChange"
          @row-dblclick="rowDblclick"
        >
          <template #nickname="{ row }">
            <span>{{ row.zid_info ? `${row.nickname}【${row.zid_info.nickname}】` : row.nickname }}</span>
          </template>
          <template #pay_type="{ row }">
            <span :class="`pay_type${row.is_prior_payment}`">
              {{ row.is_prior_payment == 0 ? "货到付款" : "在线支付" }}
            </span>
          </template>
          <template #time="{ row }">
            <span>{{ parseTime(row.created_at, "{y}-{m}-{d}") }}</span>
          </template>
          <template #date="{ row }">
            <span v-if="row.status != 0">
              {{ row.check_time != 0 ? parseTime(row.check_time, "{y}-{m}-{d}") : "" }}
            </span>
          </template>
          <template #order_pay_status="{ row }">
            <span :class="`order${row.status}`">
              {{ statusList[row.status] }}
            </span>
          </template>
          <el-table-column
            label="管理操作"
            fixed="right"
            prop="operate"
            :min-width="140"
            :resizable="false"
            class-name="main_hei"
            label-class-name="main-center"
          >
            <template slot-scope="scope">
              <router-link :to="'/order/return/see/' + scope.row.id">
                <el-link type="success">查看</el-link>
              </router-link>
              <router-link
                v-if="scope.row.status == 0"
                :to="'/order/return/check/' + scope.row.id"
              >
                <el-link type="warning">审核</el-link>
              </router-link>
              <el-link @click="print(scope.row.id)">打印</el-link>
            </template>
          </el-table-column>
        </MainTable>
        <div class="main_page boxdq">
          <div></div>
          <!-- 分页 -->
          <div class="main_page_r">
            <MainPagination
              :current-page="current.page"
              :page-sizes="[20, 50, 100, 200]"
              :page-size="limit"
              layout="total, sizes, prev, pager, next, jumper"
              :total="totalCount"
              background
              @size-change="changeLimit"
              @current-change="changePage"
            />
          </div>
        </div>

        <el-dialog
          title="导出"
          :visible.sync="exportFormVisible"
          class="main_dialog dialog500"
          @close="type = list.field"
        >
          <div class="export">你选择了{{ this.selectArr.length }}条数据</div>
          <div slot="footer" class="dialog-footer">
            <el-button @click="exportFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="exportConfim" v-repeatClick
              >确 定</el-button
            >
          </div>
        </el-dialog>
      </div>
    </div>
    <!-- 打印子组件 -->
    <order-print
      ref="printTemplate"
      v-show="false"
      id="printname"
    ></order-print>
  </div>
</template>

<script>
import { mixins } from "@/mixins/index.js";
import dateOption from "@/options/dateOption";
import { mapGetters } from "vuex";
import {
  order_return_index,
  export_order_return,
} from "@/api/order/orderReturn";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import { parseTime, exportMethod } from "@/utils/index";
import OrderPrint from "@/views/order/orderPrint.vue";
import { user_getMemberInfo } from "@/api/currency";
import { orderReturnColumns } from "./mockData.js"
export default {
  mixins: [mixins],
  components: {
    OrderPrint,
  },
  computed: {
    ...mapGetters(["avatar"]),
    reverseSum() {
      return this.percent_list.reverse();
    },
  },
  data() {
    return {
      orderReturnColumns, // 表格
      exportMethod,
      dateOption,
      isloading: true,
      // 时间
      parseTime,
      // 分页
      current: {
        page: 1,
        per_page: 20,
      },
      limit: 20,
      totalCount: 1,
      // 更多
      moredata: false,
      // 列表数据
      list: {
        data: [],
        total: "",
      },
      statusList: [],
      order_status: [],
      id: "",
      seeFormVisible: false,
      listLoading: false,
      // 搜索
      form: {
        status: null, //状态
        start_time: "", //申请开始时间
        uid: "",
        end_time: "",
        order_sn: "",
        related_order_sn: "",
        storage_order_sn: "",
        check_start_time: "", //审核开始时间
        check_end_time: "",
        goods_spu_id: "",
        zid: "",
      },
      zid_name: [],
      createTime: [],
      check_createTime: [],
      // 全选
      selectArr: [],
      exportFormVisible: false,
    };
  },

  created() {
    // 列表
    this.getNumerical(210);
    this.order_return_index();
  },
  methods: {
    //  表格双击跳转详情页面
    rowDblclick(row, column, event) {
      this.$router.push('/order/return/see/' + row.id)
    },
    // 监听全选
    handleSelectionChange(val) {
      this.selectArr = [];
      val.map((item) => {
        this.selectArr.push({ id: item.id });
      });
    },
    //清除子账号
    clearzid() {
      this.form.uid = "";
      this.form.zid = "";
      this.zid_name = [];
    },
    //展示子账号
    uidName(uid) {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { id: uid }
      );
      user_getMemberInfo(data).then((res) => {
        if (res.errcode == 0) {
          this.form.zid = "";
          this.form.zid_name = null;
          res.data.subAccount.map((e, ind) => {
            if (ind == 0) {
              e.id = 0;
              e.nickname = "主账号";
            }
          });
          res.data.subAccount.unshift({
            id: "",
            nickname: "所有",
          });
          this.zid_name = res.data.subAccount;
          this.zid_name = res.data.subAccount;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 打印
    print(id) {
      this.printid = id;
      this.$refs.printTemplate.$emit("demo", { id: id, type: "order_return" });
    },
    // 点击搜索时折叠搜索条件
    getSearchFor() {
      this.getArticleList();
    },
    // 敲击键盘的回车键时调用的函数
    startGetting() {
      this.getArticleList();
    },
    // 搜索
    getArticleList() {
      this.current.page = 1;
      this.order_return_index();
    },
    // 重置
    reset() {
      this.$refs["userListFrom"].resetFields();
      if (this.$refs["changeGoodsInput"]) {
        this.$refs["changeGoodsInput"].name = "";
      }
      if (this.$refs["changeUserInput_form"]) {
        this.$refs["changeUserInput_form"].name = "";
      }
      this.createTime = [];
      this.check_createTime = [];
      this.order_return_index();
    },
    // 更多
    more() {
      this.moredata = !this.moredata;
    },
    // 分页
    changeLimit(pageSize) {
      this.current.per_page = pageSize;
      this.order_return_index();
    },
    changePage(page) {
      this.current.page = page;
      this.order_return_index();
    },
    // 列表

    order_return_index() {
      this.listLoading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, this.current)) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.list_form(),
        this.current
      );
      order_return_index(data).then((res) => {
        if (res.errcode == 0) {
          this.list = res.data.list;
          this.statusList = res.data.statusList;
          this.order_status = res.data.order_status;
          this.totalCount = res.data.list.total;
          this.listLoading = false;
        } else {
          this.$message.errcode(res.msg);
        }
      });
    },
    list_form() {
      let form = JSON.parse(JSON.stringify(this.form));
      if (this.createTime.length > 0) {
        form.start_time = this.createTime[0];
        form.end_time = this.createTime[1];
      }
      if (this.check_createTime.length > 0) {
        form.check_start_time = this.check_createTime[0];
        form.check_end_time = this.check_createTime[1];
      }
      return form;
    },
    //订单导出
    exportMethodDia() {
      if (this.selectArr.length > 0) {
        this.exportFormVisible = true;
      } else {
        this.exportConfim();
      }
    },
    // 导出确认
    exportConfim() {
      let form = JSON.parse(JSON.stringify(this.form));
      let ids = [];
      if (this.selectArr.length > 0) {
        this.selectArr.forEach((item) => {
          ids.push(item.id);
        });
      }
      ids = ids.join("-");
      form.ids = ids;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, this.current)) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        form,
        this.current
      );
      const notify = this.$notify.info({
        title: "开始导出...",
        type: "success",
        duration: 0,
      });
      export_order_return(data).then((res) => {
        this.exportMethod(res, "订单退货", notify);
      });
      this.exportFormVisible = false;
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";

.mainsu {
  .mainor_form form {
    width: 100%;
  }
  .main_num {
    .mainsu_te {
      position: relative;
      top: -1px;
    }
  }
}
.main_dialog {
  .main_table {
    /deep/.el-table__body-wrapper {
      overflow-y: auto;
      height: 400px;
    }
  }
  .main_xq ul li {
    width: 33.33%;
    margin-top: 5px;
    margin-bottom: 5px;
  }
  .progress_list {
    margin-top: 10px;
    p {
      height: 24px;
      line-height: 24px;
    }
    .progress_list_red {
      color: red;
    }
    .progress_list_su {
      color: #03a98e;
    }
  }
}

// 订单状态
.order0 {
  color: #f56c6c;
}
.order1 {
  color: #118ab2;
}
.order2 {
  color: #aacd6e;
}
.order3 {
  color: #073b4c;
}
.order4 {
  color: #cccccc;
}
// 支付状态
.pay_type0 {
  color: #f56c6c;
}
.pay_type1 {
  color: #03a98e;
}
.btn_box {
  padding-left: 35px;
}
/deep/ .el-form-item--small.el-form-item {
  margin-bottom: 10px;
}
</style>
